layui.config({
	base : "js/"
}).use(['form','layer','jquery','laypage'],function(){
	var form = layui.form(),
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		laypage = layui.laypage,
		$ = layui.jquery;

	//加载页面数据
	var newsData = '';
	console.log("notice/list.js")
	$.get($.httpRootPath()+"/api/sys/parameter/all", function(data){
		newsData = newsData = JSON.parse(data).data;;
			//执行加载数据的方法
			newsList();
	})
	//添加文章
	//改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
	$(window).one("resize",function(){
		$(".infoAdd_btn").click(function(){
			var index = layui.layer.open({
				title : "添加公告",
				type : 2,
				content : "add.html",
				success : function(layero, index){
					setTimeout(function(){
						layui.layer.tips('点击此处返回文章列表', '.layui-layer-setwin .layui-layer-close', {
							tips: 3
						});
					},500)
				}
			})			
			layui.layer.full(index);
		})
	}).resize();

	//全选
	form.on('checkbox(allChoose)', function(data){
		var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"])');
		child.each(function(index, item){
			item.checked = data.elem.checked;
		});
		form.render('checkbox');
	});

	//通过判断文章是否全部选中来确定全选按钮是否选中
	form.on("checkbox(choose)",function(data){
		var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"])');
		var childChecked = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"]):checked')
		if(childChecked.length == child.length){
			$(data.elem).parents('table').find('thead input#allChoose').get(0).checked = true;
		}else{
			$(data.elem).parents('table').find('thead input#allChoose').get(0).checked = false;
		}
		form.render('checkbox');
	})

	//是否展示
	form.on('switch(isShow)', function(data){
		var index = layer.msg('修改中，请稍候',{icon: 16,time:false,shade:0.8});
		var _this = $(this);
		var notId = _this.attr("data-id");
		var data ={
			orgId:orgId,
			notId:notId
		}
		updateState(data,function(){
			layer.close(index);
		})
	})
 
	//操作
	$("body").on("click",".info_edit",function(){  //编辑
		var _this = $(this);
		var notId = _this.attr("data-id");
		window.sessionStorage["paramaterEditId"] = notId;
		var index = layui.layer.open({
			title : "编辑信息",
			type : 2,
//			area: ['600px', '300px'],
			content : "edit.html",
			success : function(layero, index){
				setTimeout(function(){
					layui.layer.tips('点击此处返回公告列表', '.layui-layer-setwin .layui-layer-close', {
						tips: 3
					});
				},500)
			}
		})
		//改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
		$(window).resize(function(){
			layui.layer.full(index);
		})
		layui.layer.full(index);
	})


	$("body").on("click",".info_del",function(){  //删除
		var _this = $(this);
		layer.confirm('确定删除此信息？',{icon:3, title:'提示信息'},function(index){
			//_this.parents("tr").remove();
			var data = {
				id:_this.attr("data-id"),
			}
			var del = deleteInfo(data,function(){
				layer.close(index);
			})
			if(del){
				_this.parents("tr").remove();
			}
		});
	})

	function newsList(that){
		//渲染数据
		function renderDate(data,curr){
			var dataHtml = '';
			if(!that){
				currData = newsData.concat().splice(curr*nums-nums, nums);
			}else{
				currData = that.concat().splice(curr*nums-nums, nums);
			}
			if(currData.length != 0){
				for(var i=0;i<currData.length;i++){
					var dataHt = '';
					if(currData[i].state == 1){
						dataHt = 'checked';
					}
					dataHtml += '<tr>'
			    	+'<td><input type="checkbox" name="checked" lay-skin="primary" lay-filter="choose"></td>'
			    	+'<td align="left">'+currData[i].parKey+'</td>'
			    	+'<td align="left">'+currData[i].parValue+'</td>'
			    	+'<td>'+currData[i].parType+'</td>'
			    	+ '<td>'+currData[i].sort+'</td>'
			    	+'<td>'+currData[i].createTime+'</td>'
			    	+'<td>'
					+  '<a class="layui-btn layui-btn-mini info_edit" data-id="'+currData[i].id+'"><i class="iconfont icon-edit"></i> 编辑</a>'
					+  '<a class="layui-btn layui-btn-danger layui-btn-mini info_del" data-id="'+currData[i].id+'"><i class="layui-icon">&#xe640;</i> 删除</a>'
			        +'</td>'
			    	+'</tr>';
				}
			}else{
				dataHtml = '<tr><td colspan="8">暂无数据</td></tr>';
			}
		    return dataHtml;
		}

		//分页
		var nums = 13; //每页出现的数据量
		if(that){
			newsData = that;
		}
		laypage({
			cont : "page",
			pages : Math.ceil(newsData.length/nums),
			jump : function(obj){
				$(".news_content").html(renderDate(newsData,obj.curr));
				$('.news_list thead input[type="checkbox"]').prop("checked",false);
		    	form.render();
			}
		})
	}
})
function deleteInfo(data,callback) {
	var del = false;
    $.ajax({
        url: $.httpRootPath()+"/api/sys/parameter/"+data.id,
        type: 'DELETE',
        dataType: 'json',
        data: data,
        timeout: 15000,
        cache: false,
        async:false,
        success: function (r) {
            if (r.code == "00") {
            	del = true;
              	layer.msg("删除成功！");
            } else {
               layer.msg(r.msg);
            }
        },
        error: function (e) {
          layer.msg("系统错误！");
        },
        complete:function(){
        		callback();
        }
    });
    return del;
   }
function updateState(data,callback) {
    $.ajax({
        url: $.httpRootPath()+"/api/sys/notice/i/"+data.orgId+"/"+data.notId+'/state',
        type: 'PUT',
        dataType: 'json',
        data: data,
        timeout: 15000,
        cache: false,
        success: function (r) {
            if (r.code == "00") {
              	layer.msg("更新成功！");
            }else {
               layer.msg(r.msg);
            }
            callback();
        },
        error: function (e) {
          layer.msg("系统错误！");
        }
    });
   }
